
/**
** \file       while.c
**
** \brief      this function parse the while rule
**
** \author     Ben M'rad Hanna, Boillot Ulysse, De Olivera Guillaume
**             Laterem Hamza, and Nguyen Viet Dung
**
** \version    0.8
**
** \date       26 November 2010
**
*/

#include "rules.h"

static s_rulewhile              *my_free1(s_rulewhile                   *rwhile,
                                          int                           n)
{
  if (n == 1)
  {
    free(rwhile->in_while);
    rwhile->in_while = NULL;

    return (NULL);
  }
  if (n == 2)
  {
    free(rwhile->in_do);
    rwhile->in_do = NULL;

    return (NULL);
  }
  return (NULL);
}

s_rulewhile                     *rule_while(s_rulewhile                 *rwhile)
{
  if (g_data->lexer == NULL)
    return (NULL);

  if (strcmp(g_data->lexer->token->id, "while") == 0)
  {
    g_data->lexer = g_data->lexer->next;

    if (g_data->lexer == NULL)
      return (NULL);

    rwhile->in_while = calloc(1, sizeof (s_cmpdlist));

    if (rule_compound_list(rwhile->in_while) == NULL)
      return (my_free1(rwhile, 1));

    if (g_data->lexer == NULL)
    {
      my_free1(rwhile, 1);
      return (my_free1(rwhile, 2));
    }

    rwhile->in_do = calloc(1, sizeof (s_dogroup));

    if (rule_do_group(rwhile->in_do) == NULL)
      return (my_free1(rwhile, 2));

    return (rwhile);
  }
  return (NULL);
}
